package com.bhs.sansonglogistics.ui.wallet;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.BusinessAdministrationBean;
import com.bhs.sansonglogistics.bean.CompanyApproveInfoBean;
import com.bhs.sansonglogistics.bean.RedEnvelopeRule;
import com.bhs.sansonglogistics.bean.UserInfoBean;
import com.bhs.sansonglogistics.bean.event.CompanyInfo;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.net.Config;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.ui.enterprise.AddLineActivity;
import com.bhs.sansonglogistics.ui.enterprise.EnterpriseCertificationActivity;
import com.bhs.sansonglogistics.ui.enterprise.EnterpriseCollaborationActivity;
import com.bhs.sansonglogistics.ui.other.WebViewActivity;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.SharedPreferencesUtils;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import okhttp3.ResponseBody;
import retrofit2.Response;

public class MyRedEnvelopeActivity extends BaseActivity implements View.OnClickListener, NetCallBack {

    private TextView mTvRecord;
    private TextView mTvMoney;
    private TextView mTvHint;
    private TextView mTvSettledEnterprise;
    private TextView mBtnSettledEnterprise;
    private TextView mTvLine;
    private TextView mBtnLine;
    private TextView mTvOrderReceiving;
    private TextView mBtnOrderReceiving;
    private TextView mTvAgreement;
    private TextView mTvRule;
    private String balance;
    private TextView mTvClearingTime;
    private TextView mTvTransferBalance;

    @Override
    public int initLayout() {
        return R.layout.activity_my_red_envelope;
    }

    @Override
    public void initView() {

        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("我的红包");
        mTvRecord = findViewById(R.id.tv_record);
        mTvRecord.setOnClickListener(this);
        mTvMoney = findViewById(R.id.tv_money);
        mTvHint = findViewById(R.id.tv_hint);
        mTvSettledEnterprise = findViewById(R.id.tv_settled_enterprise);
        mBtnSettledEnterprise = findViewById(R.id.btn_settled_enterprise);
        mTvLine = findViewById(R.id.tv_line);
        mBtnLine = findViewById(R.id.btn_line);
        mTvOrderReceiving = findViewById(R.id.tv_order_receiving);
        mBtnOrderReceiving = findViewById(R.id.btn_order_receiving);
        mTvAgreement = findViewById(R.id.tv_agreement);
        mTvRule = findViewById(R.id.tv_rule);
        mBtnLine.setOnClickListener(this);
        mBtnSettledEnterprise.setOnClickListener(this);
        mBtnOrderReceiving.setOnClickListener(this);
        mTvClearingTime = findViewById(R.id.tv_clearing_time);
        mTvTransferBalance = findViewById(R.id.tv_transfer_balance);
        mTvTransferBalance.setOnClickListener(this);
    }

    @Override
    public void initData() {
        String str = "详细规则请阅读《红包使用规则》";
        SpannableStringBuilder spannableBuilder = new SpannableStringBuilder(str);
        // 在设置点击事件、同时设置字体颜色
        ClickableSpan clickableSpanOne = new ClickableSpan() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mActivity, WebViewActivity.class)
                        .putExtra("url", Config.getRedEnvelopeRule())
                        .putExtra("title", "红包规则"));
            }

            @Override
            public void updateDrawState(TextPaint paint) {
                paint.setColor(Color.parseColor("#485EF4"));
                // 设置下划线 true显示、false不显示
                paint.setUnderlineText(false);
            }
        };
        spannableBuilder.setSpan(clickableSpanOne, 7, 15, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTvAgreement.setMovementMethod(LinkMovementMethod.getInstance());
        mTvAgreement.setText(spannableBuilder);
    }

    @Override
    protected void onResume() {
        super.onResume();
        networkRequest(netApi.get_user_info(), this, 666);
        networkRequest(netApi.getIndexInfo(), this, 888);
        networkRequest(netApi.getCompanyApproveInfo(), this, 999);
        networkRequest(netApi.get_redpack_config("logistics"), this, 777);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_record:
                startActivity(new Intent(mActivity, RedEnvelopeRecordActivity.class));
                break;
            case R.id.tv_transfer_balance:
                startActivity(new Intent(mActivity, RedEnvelopeTransferBalanceActivity.class)
                        .putExtra("balance", balance));
                break;
            case R.id.btn_line:
                if (!mBtnLine.getText().toString().equals("已完成")) {
                    startActivity(new Intent(mActivity, AddLineActivity.class));
                }
                break;
            case R.id.btn_settled_enterprise:
                if (!mBtnSettledEnterprise.getText().toString().equals("已完成")) {
                    startActivity(new Intent(mActivity, EnterpriseCollaborationActivity.class));
                }
                break;
            case R.id.btn_order_receiving:
                finish();
                EventBus.getDefault().post(new Event(0));
                break;
        }

    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        if (requestCode == 999) {
            try {
                //判断是否已经认证
                CompanyApproveInfoBean companyApproveInfoBean = new Gson().fromJson(json, CompanyApproveInfoBean.class);
                if (companyApproveInfoBean.isStatus()) {
                    if (companyApproveInfoBean.getData().getCompany_approve_info().getStatus() == 1) {
                        mBtnSettledEnterprise.setBackgroundResource(R.drawable.bg_accomplish);
                        mBtnSettledEnterprise.setText("已完成");
                    }
                }
            } catch (Exception e) {

            }

        } else if (requestCode == 888) {
            //判断是否有专线
            BusinessAdministrationBean bean = new Gson().fromJson(json, BusinessAdministrationBean.class);
            if (bean.isStatus()) {
                if (!bean.getData().getLine_num().equals("0")) {
                    mBtnLine.setBackgroundResource(R.drawable.bg_accomplish);
                    mBtnLine.setText("已完成");
                }
            }
        } else if (requestCode == 777) {
            RedEnvelopeRule infoBean = new Gson().fromJson(json, RedEnvelopeRule.class);
            if (infoBean.isStatus()) {
                mTvClearingTime.setText(String.format("清零时间：%s", DateUtils.getYearMonthDay(infoBean.getData().getNextClearTime() * 1000L)));
                mTvRule.setText(Html.fromHtml(infoBean.getData().getRuleStr()));
                if (infoBean.getData().getZhuanyueeSwitch().equals("1")) {
                    mTvTransferBalance.setVisibility(View.VISIBLE);
                } else {
                    mTvTransferBalance.setVisibility(View.INVISIBLE);
                }
            }
        } else if (requestCode == 666) {
            UserInfoBean infoBean = new Gson().fromJson(json, UserInfoBean.class);
            if (infoBean.isStatus()) {
                CompanyInfo companyInfo = infoBean.getData().getLogistics_info();
                mTvMoney.setText(companyInfo.getMoney02());
                balance = companyInfo.getMoney02();
            }
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}